<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri='http://www.springframework.org/security/tags' prefix='sec' %>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../../inc/inc.jsp"></jsp:include>
		<script type="text/javascript">
			$(document).ready(function(){		
				$('#accessType').combobox({
					onChange: function (n,o) {
						loadAccess('info_data?access_type='+n);
					}
				});
				$('#accessType').combobox('setValue',['1']);
			});
			function loadAccess(accessUrl){
				$('#treeGrid').treegrid({
					url : accessUrl,
					idField : 'id',
					treeField : 'name',
					parentField : 'parentId',
					fit : true,
					fitColumns : false,
					border : false,
					loadMsg:'正在加载数据...',
					columns : [ [ {
						field : 'name',
						title : '资源名称',
						width : 220,
						halign : 'center'
					}, {
						field : 'path',
						title : '资源路径',
						width : 250,
						halign : 'center'
					}, {
						field : 'code',
						title : '资源code',
						width : 250,
						halign : 'center'
					}, {
						field : 'icon',
						title : '图标',
						width : 130,
						halign : 'center'
					},{
						field : 'orderNo',
						title : '排序',
						width : 90,
						halign : 'center'
					},{
						field : 'accessType',
						title : '资源类型',
						width : 90,
						halign : 'center',
						formatter : function(value, row, index) {
							switch (value) {
							case "0":
								return '菜单';
							case "1":
								return '按钮';
							case "2":
								return '基础资源';
							}
						}
					}, {
						field : 'parentId',
						title : '上级资源ID',
						width : 150,
						hidden : true
					}, {
						field : 'status',
						title : '状态',
						width : 90,
						halign : 'center',
						formatter : function(value, row, index) {
							switch (value) {
							case "0":
								return '正常';
							case "1":
								return '停用';
							}
						}
					}] ],
					toolbar : '#toolbar'
				});
			}
		</script>
	</head>
	<body>
		<!-- 列表 -->
		<div class="easyui-layout" data-options="fit:true,border:false">
			<div data-options="region:'center',border:false">
				<table id="treeGrid"></table>
			</div>
		</div>
		<div id="toolbar" style="display:none;">
			<sec:authorize ifAnyGranted='B_ZYGL_ADD'>
				<a onclick="addAccess();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">增加</a>
			</sec:authorize>
			<sec:authorize ifAnyGranted='B_ZYGL_EDIT'>
				<a onclick="editAccess();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">修改</a>
			</sec:authorize>
			<sec:authorize ifAnyGranted='B_ZYGL_DEL'>
				<a onclick="delAccess();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
			</sec:authorize>
			<div style="padding-top:2px;padding-right:20px; float:right;">资源类型:
				<select class="easyui-combobox" id="accessType" name="type"  data-options="width:100,editable:false,panelHeight:'auto'">
					<option value="0">菜单</option>
					<option value="1">菜单+按钮</option>
					<option value="2">全部</option>
				</select>
			</div>
		</div>
		
		<!-- 编辑 -->
		<div id="dlg" class="easyui-dialog" style="width:700px;height:auto;padding:10px 20px" buttons="#dlg-buttons" data-options="closed:true,modal:true">
			<form id="fm" method="post">
				<input type="hidden" name="id"/>
				<input type="hidden" id="parent_id" name="parentId"/>
				<table class="grid">
					<tr>
						<td>名称</td>
						<td><input id="name" name="name" type="text" class="easyui-textbox" data-options="required:true,validType:['length[0,30]']" /></td>
						<td>图标</td>
						<td>
							<select name="icon" class="easyui-combobox" data-options="width:100,height:25,editable:false,panelHeight:'auto'">
								<option value="icon-1menu">一级菜单</option>
								<option value="icon-2menu">二级菜单</option>
								<option value="icon-3menu">三级菜单</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>URL</td>
						<td colspan="3">
							<input name="path" type="text" class="easyui-textbox" style="width:300px;" data-options="validType:['length[0,60]']" />
						</td>
					</tr>
					<tr>
						<td>类型</td>
						<td>
							<select name="accessType" class="easyui-combobox" data-options="width:100,height:25,editable:false,panelHeight:'auto'">
								<option value="0">菜单</option>
								<option value="1">按钮</option>
								<option value="2">基础资源</option>
							</select>
						</td>
						<td>排序</td>
						<td><input name="orderNo" value="0"  class="easyui-numberspinner" data-options="width:100,height:25,max:10000,min:0"></td>
					</tr>
					<tr>
						<td>权限CODE</td>
						<td><input name="code" class="easyui-textbox" data-options="required:true" /></td>
						<td>是否启用</td>
						<td>
							<select name="status" class="easyui-combobox" data-options="width:100,height:25,editable:false,panelHeight:'auto'">
								<option value="0">正常</option>
								<option value="1">停用</option>
							</select>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div id="dlg-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c8" iconCls="icon-ok" onclick="saveAccess()" style="width:90px">保存</a>
			<a href="javascript:void(0)" class="easyui-linkbutton c2" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
		</div>
	</body>
</html>

<script type="text/javascript">
	//==========================资源增删改======================
	function saveAccess(){
		var data = form2Json("fm");
	    if($("#fm").form('validate')){
	        $.post('save_update',{data:data},function(result) {
	            if(result.success){
	            	$('#treeGrid').treegrid('reload');
	            	$('#dlg').dialog('close');
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	            }else{
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	            }
	        },'json');
	    }
	}
	
	function addAccess(){
		var row = $('#treeGrid').treegrid('getSelected');
		if(row){
			$('#dlg').dialog('open').dialog('setTitle','新增资源');
			$('#fm').form('clear');
			$('#fm').form('load',{
				accessType:0,
				orderNo:0,
				status:0
			});
			$('#parent_id').val(row.id);
		}else{
			$.messager.alert('提示','请选择上级资源','info');
		}
	}
	
	function editAccess(){
		var row = $('#treeGrid').treegrid('getSelected');
		if (row){
			$('#dlg').dialog('open').dialog('setTitle','编辑资源');
			$('#fm').form('load',row);
		}else{
			$.messager.alert('提示','请选择一条记录','info');
		}
	}

	function delAccess(){
		var row = $('#treeGrid').treegrid('getSelected');
		if (row){
			$.messager.confirm('提示','确定删除?',function(r){
				if (r){
					$.ajax({
						url:'delete',
						type:'POST',
						dataType:'json',
						data:{id:row.id},
						success: function(result){
		                	if(result.success) {
								$.messager.show({
									title: '提示',
									msg: result.msg
								});
								$('#treeGrid').treegrid('reload');
		                	} else {
		                		$.messager.show({
									title: '提示',
									msg: result.msg
								});
		                	}
						}
					});
				}
			});
		}else{
			$.messager.alert('提示','请选择一条记录','info');
		}
	}
</script>